package com.weige.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg) {
        System.out.println("消息监听触发");
        System.out.println(1 / 0);
        System.out.println("消费者接收到simple.queue的消息：【" + msg + "】");
    }

    /**
     * 监听死信队列
     *
     * @param meg 消息
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "dl.queue", durable = "true"),
            exchange = @Exchange(name = "dl.direct", durable = "true", autoDelete = "false", type = ExchangeTypes.DIRECT),
            key = "dl"))
    public void listenDlQueue(String meg) {
        log.info("接送到dl.queue的延迟消息:{}", meg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.direct", durable = "true", autoDelete = "false", type = ExchangeTypes.DIRECT,delayed = "true"),
            key = "delay"))
    public void listenDelayQueue(String meg) {
        log.info("接送到dl.queue的延迟消息:{}", meg);
    }
}
